package demo.huodongbaoming.service.impl;

import demo.huodongbaoming.dto.ResponseResult;
import demo.huodongbaoming.service.DemoCircuitBreakerService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class DemoCircuitBreakerServiceImpl implements DemoCircuitBreakerService {
    private static final Logger logger = LoggerFactory.getLogger(DemoCircuitBreakerServiceImpl.class);

    @Override
    @io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker(name = "activity-service", fallbackMethod = "fallback")
    public ResponseResult<String> callWithCircuitBreaker(boolean fail) {
        logger.info("[熔断演示] 开始调用 callWithCircuitBreaker, fail={} ...", fail);
        if (fail) {
            logger.error("[熔断演示] 业务异常，触发熔断");
            throw new RuntimeException("业务异常，触发熔断");
        }
        logger.info("[熔断演示] 业务调用成功，返回正常数据");
        return ResponseResult.success("熔断演示：服务调用成功", "演示数据");
    }

    public ResponseResult<String> fallback(boolean fail, Throwable t) {
        logger.warn("[熔断演示] 进入降级 fallback，fail={}, 原因：{}", fail, t.getMessage());
        return ResponseResult.fail(500, "熔断降级：服务不可用，原因：" + t.getMessage());
    }
} 